package com.chat.api.utils.util;


public class NumberUtil {

    public static String doubeToStr(Double d) {
       /* if(d==null){
            return "0.00";
        }
        return cn.hutool.core.util.NumberUtil.decimalFormat("0.00", cn.hutool.core.util.NumberUtil.round(d, 2).doubleValue());
        */
       return doubeFormatToStr(d,"0.00");
    }

    /**
     * 按照四位小数点返回string
     * @param d
     * @return
     */
    public static String doubeFormatToStr(Double d) {
        return doubeFormatToStr(d,"0.0000");
    }

    public static String doubeFormatToStr(Double d,String pattern) {
        if(d==null){
            return pattern;
        }
        return cn.hutool.core.util.NumberUtil.decimalFormat(pattern, cn.hutool.core.util.NumberUtil.round(d, 4).doubleValue());
    }

    public static String doubeToIntStr(Double d) {
        if(d==null){
            return "0";
        }
        return cn.hutool.core.util.NumberUtil.decimalFormat("0", cn.hutool.core.util.NumberUtil.round(d, 0).doubleValue());

    }

    public static Double strToDouble(String s) {
        Double d;
        try{
            d = Double.parseDouble(s);
        }catch (Exception e){
            d= 0.00;
        }
        return d;

    }

    public static String doubeToMillionStr(Double d) {
        if(d==null){
            return "0.00";
        }
        return cn.hutool.core.util.NumberUtil.decimalFormat("0.00", cn.hutool.core.util.NumberUtil.round(d/10000, 2).doubleValue());

    }


    public static String formatToMillionStr(String d){
        return doubeToMillionStr(strToDouble(d));
    }

    public static String formatDoubleStr(String d){
        return doubeToStr(strToDouble(d));
    }
    public static String formatDoubleStrToIntStr(String d){
        return doubeToIntStr(strToDouble(d));
    }

    public static Double calPercent(Double dividend, Double divisor){
        return divisor.equals(0.0)?0:(dividend*100.00000)/(divisor*1.00000);
    }

    public static String calPercent(String dividend, String divisor){
        return doubeToStr(strToDouble(divisor).equals(0.0)?0:(strToDouble(dividend)*100.00000)/(strToDouble(divisor)*1.00000));
    }

    public static double sub(double v1, double v2){
        return cn.hutool.core.util.NumberUtil.sub(v1,v2);
    }

    public  static String sub(String v1,String v2){
        return NumberUtil.doubeToStr(sub(NumberUtil.strToDouble(v1),NumberUtil.strToDouble(v2)));
    }

    public static String plus(String v1,String v2) {
        return NumberUtil.doubeToStr(NumberUtil.strToDouble(v1) + NumberUtil.strToDouble(v2));
    }


    public static String decimalFormat(Double n){
        return cn.hutool.core.util.NumberUtil.decimalFormat(",###", n);
    }

    public static String decimalFormat(String n){
        return cn.hutool.core.util.NumberUtil.decimalFormat(",###", strToDouble(n));
    }
}
